package com.fl.iocaop.java8;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class TestLambda {
    //Consumer函数
    @Test
    public void test1(){
    Consumer(1000.0,(t)-> System.out.println("接收参数："+t));
    }

    public void Consumer(Double num,Consumer<Double> con){
         con.accept(num);
    }

    //Supplier函数(供及型函数)
    @Test
    public void test2(){
        List<Integer> list = getList(5, ()-> {
            double v = Math.random() * 100;
            Integer i = Integer.valueOf((int) v);
            System.out.println(i);
            return i;
        });
//        list.forEach(System.out::println);
    }

    public List<Integer> getList(int num,Supplier<Integer> supplier){
        List<Integer> list = new ArrayList<>();
        for(int i =0;i<num;i++){
            Integer n = supplier.get();
            list.add(n);
        }
        return list;
    }

    //function<T,R>函数方法
    @Test
    public void  test3(){
        System.out.println(strHandler("我是一个超级好人！", (str) -> str.substring(4, 9)));
    }

    public String strHandler(String str, Function<String,String> fun){

        return fun.apply(str);
    }
    //Predicate<T>断言函数方法

    @Test
    public void test4(){
        List<String> list = Arrays.asList("哈哈哈","啊我说的空间ha","32465431");
        List<String> strings = PredicateList(list, ls -> ls.length() > 3);
        strings.forEach(System.out::println);
    }

    public List<String> PredicateList(List<String> list,Predicate<String> pre){
        List<String> list1 = new ArrayList<>();
        for (String s:list){
            if (pre.test(s)){
                list1.add(s);
            }
        }
        return list1;
    }
}
